
//用户的持久层
// 1 导入用户的模型
let stuModel = require("../model/stu");


//导入管理员模型
let logModel = require("../model/login");

//暴露持久层方法给业务层调用

module.exports = {
    //登录
    async getlog({ user, pwd }) {
        let result = await logModel.findOne({ user, pwd }).exec();
        return result;
    },
    //查询管理员账号是否存在
    async logcha({ user }) {
        let result = await logModel.findOne({ user }).exec();
        return result;
    },
    //添加管理员账号
    async logAdd({ user, pwd }) {
        let result = await logModel.create({ user, pwd });
        return result;
    },
//===========================================================================================

    //分页查询学生
    async getuser(currentPage, pageSize) {
        let result = await stuModel.find().populate('classId').skip((currentPage - 1) * pageSize).limit(pageSize).sort({ '_id': -1 }).exec();
        return result;
    },

    //数据总条数
    async getTolNum() {
        let result = await stuModel.countDocuments().exec();
        return result;
    },


    //添加学生
    async insertUser(obj) {
        let result = await stuModel.create(obj);
        return result;
    },

    //删除学生
    async remove({ _id }) {
        let result = await stuModel.deleteOne({ _id }).exec();
        return result;
    },

    //批量删除学生   传入_id数组   例：Model.remove({ _id: { $in: [‘aID’, ‘bID’] } });
    async deletions(arr) {
        let result = await stuModel.remove({ _id:{$in:arr} }).exec();
        return result;
    },

    //修改学生
    async updateStu(stuId, stuObj) {
        let result = await stuModel.findByIdAndUpdate(stuId, stuObj).exec();
        return result;
    },

    //根据编号查询学生
    async stucha(stuId) {
        let result = await stuModel.findById(stuId).exec();
        return result;
    },

    //用户根据类型查询学生
    async stuTypes(obj) {
        let result = await stuModel.find(obj).populate('classId').exec();
        return result;
    },
    
}